package io.github.maojian.map;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: jian.mao
 * @Date: 2022/6/13 11:26
 * @Description: https://leetcode.cn/problems/find-and-replace-pattern/
 */
public class Q890 {
    public List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> matchList = new ArrayList<>();
        for (String word : words) {
            if (match(word, pattern)) {
                matchList.add(word);
            }
        }
        return matchList;
    }

    public boolean match(String word, String pattern) {
        return mathString(word, pattern) && mathString(pattern, word);
    }

    public boolean mathString(String s1, String s2) {
        Map<Character, Character> map = new HashMap<>();
        int length = s1.length();
        for (int i = 0; i < length; i++) {
            char key = s1.charAt(i);
            char value = s2.charAt(i);
            if (!map.containsKey(key)) {
                map.put(key, value);
            } else {
                if (map.get(key) != value) {
                    return false;
                }
            }
        }
        return true;
    }

    @Test
    public void test() {
        String[] words = new String[]{"abc", "deq", "mee", "aqq", "dkd", "ccc"};
        String pattern = "abb";
        findAndReplacePattern(words, pattern).stream().forEach(System.out::println);

    }
}
